<script lang="ts">
import { computed, defineComponent, h, unref } from 'vue'
import BasicButton from './BasicButton.vue'
import { Popconfirm } from 'ant-design-vue'
import { extendSlots } from '/@/utils/helper/tsxHelper'
import { omit, pickBy } from 'lodash-es'
import { useAttrs } from '/@/hooks/core/useAttrs'
import { useI18n } from '/@/hooks/web/useI18n'
const props = {
  /**
   * Whether to enable the drop-down menu
   * @default: true
   */
  enable: {
    type: Boolean,
    default: true,
  },
}

export default defineComponent({
  name: 'PopButton',
  inheritAttrs: false,
  props,
  setup(props, { slots }) {
    const attrs = useAttrs()
    const { t } = useI18n()
    // get inherit binding value
    const getBindValues = computed(() => {
      return Object.assign(
        {
          okText: t('common.okText'),
          cancelText: t('common.cancelText'),
        },
        { ...props, ...unref(attrs) }
      )
    })

    return () => {
      const bindValues = omit(unref(getBindValues), 'icon')
      const btnBind = omit(bindValues, 'title') as Recordable

      // 获取对应的事件，以防止事件为空的情况发生报错
      const on = {
        click: btnBind.onClick,
        confirm: bindValues.confirm,
        cancel: bindValues.cancel,
      }
      if (btnBind.disabled) btnBind.color = ''
      const Button = h(BasicButton, {
        props: omit(btnBind, ['onClick']),
        on: pickBy(on),
        scopedSlots: extendSlots(slots),
      })
      // If it is not enabled, it is a normal button
      if (!props.enable) {
        return Button
      }
      return h(
        Popconfirm,
        {
          props: omit(bindValues, ['onClick', 'confirm', 'cancel']),
          on: pickBy(on),
        },
        [Button]
      )
    }
  },
})
</script>
